package org.yi.web.account;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.core.annotation.Action;
import org.yi.core.common.Constants;
import org.yi.core.enums.RoleEnum;
import org.yi.web.account.entity.AccountEntity;
import org.yi.web.base.BaseController;

@Action(action = "/user")
public class UserController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	/**
	 * 进入账户管理首页
	 */
	public void index() {
		render("/template/themes/"+getSite().get("theme")+"/index.html");
	}

	public void goLogin() {
		render("/template/themes/"+getSite().get("theme")+"/login.html");
	}
	
	public void login() {
		String username = getPara("username");
		String password = getPara("password");
		String redirect = getPara("redirect");
        try {
        	AccountEntity account = AccountEntity.dao.getByName(username);
            if(account == null){
            	addError("message", "用户名不存在!");
            	forwardAction("/user/goLogin");
            } else if(!DigestUtils.md5Hex(password +"{" + username + "}").equals(account.get("passwd"))) {
            	addError("message", "密码错误!");
            	forwardAction("/user/goLogin");
            } else {
            	getSession().setAttribute(Constants.FRONT_LOGIN_USER, account);
            	redirect = StringUtils.isBlank(redirect) ? "/" : redirect;
            	redirect(redirect);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            addError("message", "unknow error:" + e.getMessage());
        	forwardAction("/user/goLogin");
		}
	}
	
	public void goRegist() {
		render("/themes/"+getSite().get("theme")+"/regist.html");
	}
	
	public void regist(){
		String userName = getPara("username");
		String email = getPara("email");
		String password = getPara("password");
		try {
			if(AccountEntity.dao.getByName(userName) != null) {
				addError("message", "用户 [" + userName + "] 已存在!");
            	forwardAction("/user/goRegist");
			} else if(AccountEntity.dao.getByEmail(email) != null) {
				addError("message", "邮箱 [" + email + "] 已存在!");
				forwardAction("/user/goRegist");
			} else if(StringUtils.isBlank(password)) {
				addError("message", "密码不能为空!");
				forwardAction("/user/goRegist");
			} else {
				AccountEntity account = new AccountEntity();
				account.set("login_name", userName);
				account.set("passwd", password);
				account.set("email", email);
				account.set("role", RoleEnum.REGISTER_USER.getCode());
				account.set("passwd", DigestUtils.md5Hex(password +"{" + userName + "}"));
				account.save();
				getSession().setAttribute(Constants.FRONT_LOGIN_USER, account);
				forwardAction("/");
			}
		} catch (Exception e) {
			logger.error("注册新用户 [" + userName + "] 失败，" + e.getMessage(), e);
			addError("message", "注册新用户 [" + userName + "] 失败!");
			forwardAction("/user/goRegist");
		}
	}
	
	public void findPwd() {
		// TODO
	}
	
	public void updatePwd() {
		// TODO
	}
	
	/**
	 * 我的评论页面
	 */
	public void comments() {
		// TODO 我的评论列表
	}
	
	public void logout() {
		getSession().invalidate();
		redirect("/");
	}
}
